import qs from "qs";
import baseUrl from "./path.js"
let BASE_URL;


export const myRequest = (options) => {
	let token = uni.getStorageSync('access_token');
	const requestData = formatParams(options.data);
	return new Promise((resolve, reject) => {
		if (!options.text) {
			uni.showLoading({
				title: '加载中'
			});
		} else {
			uni.showLoading({
				title: options.text
			});
		}
		const baerer = token ? token : '';
	    uni.request({
	    	url: baseUrl.wy + options.url,
	    	header: {
	    		'Content-Type': 'application/json',
	    		'Authorization':'Bearer ' + baerer
	    	},
	    	method: options.method || 'GET',
	    	data: requestData,
	    	success: (res) => {
	    		uni.hideLoading();
	    		// console.log(res);
	    		const {
	    			status,
	    			data,
	    			msg
	    		} = res.data;
	    		
	    		if (status == 200) {
	    			resolve(res.data)
	    		} else {
	    			uni.showToast({
	    				icon: 'none',
	    				title: msg + options.url
	    			})
	    			return
	    		}
	    	},
	    	fail: (err) => {
	    		console.log(err)
	    		reject(err)
	    	}
	    })

	})

}

function formatParams(params) {
	// const token = uni.getStorageSync("token") ? uni.getStorageSync("token") : "";
	// if (!params) {
	// const params = {
	// 		token: token
	// 	}
	// } else {
	// 	params.token = token;
	// }
	//return qs.stringify({param: params})
	return params
}
